package com.mm.learn.mq;/**
 * @author wdx
 * @create 2021-07-28 16:16
 */

import com.mm.learn.shopping.order.vo.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.UUID;

/**
 * @author wdx
 * @date 2021年07月28日 16:16
 */
@Controller
public class Recive {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    @ResponseBody
    public void sendMessage(){
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setOrderSn(UUID.randomUUID().toString());
        rabbitTemplate.convertAndSend("order-event-exchange","order.create.order",orderEntity);
    }

    @RabbitListener(queues = {"order.delay.queue"})
    public void recieveMessage(Message message, OrderEntity userInfoTo, Channel channel) {
        System.out.println(System.currentTimeMillis() + userInfoTo.toString());
        try {
//            int i = 10/0;
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }
}
